package com.zz.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @description  单例模式创建sqlsessionfactory  
 * @author       keep  
 * @date         2019年1月31日
 */
public class SqlSessionFactoryUtil {
	private static SqlSessionFactory primarySqlSessionFactory;
	private static SqlSessionFactory secoundSqlSessionFactory;
	private static final String READER="mybatis-config.xml";
	private static final String PRIMARYENVIRONMENT="development";
	private static final String SECOUNDENVIRONMENT="secound";
    	
	
	public static SqlSessionFactory getPrimarySqlSessionFactory() {
		if(primarySqlSessionFactory==null) {
			InputStream inputStream;
			try {
				inputStream = Resources.getResourceAsStream(READER);
				primarySqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream,PRIMARYENVIRONMENT);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return primarySqlSessionFactory;
	}
	
	public static SqlSessionFactory getSecoundSqlSessionFactory() {
		if(secoundSqlSessionFactory==null) {
			InputStream inputStream;
			try {
				inputStream = Resources.getResourceAsStream(READER);
				secoundSqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream,SECOUNDENVIRONMENT);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return secoundSqlSessionFactory;
	}
	
}
